package com.zzld.modules.autoinn.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.CookieUtils;
import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UploadFileUtil;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.autoinn.entity.Store;
import com.zzld.modules.autoinn.service.IStoreService;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.common.dto.TreeGrid;

@Controller
@RequestMapping("/autoinn/store")
public class StoreController {

	@Autowired
	private IStoreService storeService;

	private String listurl = "/modules/autoinn/store/storeList";
	private String cururl = "/modules/autoinn/store/storeCru";

	@RequestMapping(method = RequestMethod.GET)
	public String store(Model model, HttpServletRequest request) {

		return listurl;
	}

	@RequestMapping(value = "cru")
	public String cu(Model model, HttpServletRequest request) {
		return cururl;
	}

	/*
	 * 删除操作
	 */
	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON delete(HttpServletRequest request,
			@PathVariable("id") String id) {
		CommonJSON json = new CommonJSON();
		if (id != null && !id.equals("")) {

			if (storeService.selectprocount(id).equals(0)) {
				if (storeService.deletebykey(id) > 0) {
					json.setMessage(MessageUtils.DELETE_SUCCESS);
					json.setSuccess(true);
				} else {
					json.setMessage(MessageUtils.DELETE_ALL_WARNING);
					json.setSuccess(false);
				}
			}else{
				json.setMessage("此门店存在商品信息，请先删除商品信息!");
				json.setSuccess(false);
			}
		} else {
			json.setMessage("出现异常!");
			json.setSuccess(false);
		}
		return json;
	}

	@RequestMapping(value = "update/pro", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON uporadd(HttpServletRequest request, Store store,
			Model model) throws IOException {
		CommonJSON json = new CommonJSON();

		int result = 0;
		// 判断id是否为空
		if (StringUtils.isBlank(store.getId())) { // 新增
			store.setId(UuidUtil.uuid().toString());
			store.setOpentime(new Date());
			try {
				result = storeService.upOraddstore(store, 1);
				if (result == 1) {
					json.setMessage(MessageUtils.ADD_SUCCESS);
					json.setSuccess(true);
				} else {
					json.setMessage(MessageUtils.ADD_FAIL);
					json.setSuccess(false);
				}
			} catch (Exception e) {
				e.printStackTrace();
				json.setMessage(MessageUtils.ADD_FAIL);
				json.setSuccess(false);
			}
		} else {
			try {
				store.setAreaname("");
				
				result = storeService.upOraddstore(store, 0);
				if (result == 1) {
					json.setMessage(MessageUtils.EDIT_SUCCESS);
					json.setSuccess(true);
				} else {
					json.setMessage(MessageUtils.EDIT_WARING);
					json.setSuccess(false);
				}
			} catch (Exception e) {
				e.printStackTrace();
				json.setMessage(MessageUtils.EDIT_WARING);
				json.setSuccess(false);
			}
		}

		return json;
	}

	@RequestMapping(value = "ser/json", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject sebyid(HttpServletRequest request, Model model) {
		JSONObject json = new JSONObject();
		String id = request.getParameter("id");
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.putAll(storeService.selecthoukey(id));
			json.putAll(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}

	@RequestMapping(value = "sellist", method = RequestMethod.POST)
	@ResponseBody
	public List<TreeGrid> serlist(HttpServletRequest request) {

		return storeService.treeGrid();
	}

	@RequestMapping(value = "list")
	@ResponseBody
	public CommonJSON list(HttpServletRequest request,
			HttpServletResponse response, Store entity, Model model) {
		CommonJSON result = new CommonJSON();
		int pageNum = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("rows"));
		String sort = request.getParameter("sort") == "seq" ? "name" : request
				.getParameter("sort");
		String order = request.getParameter("order");
		String name = request.getParameter("storename");
		String state = request.getParameter("state");
		String address = request.getParameter("seaaddress");
		String startime = request.getParameter("startime");
		String endtime = request.getParameter("endtime");
		String areaid = request.getParameter("arealistid");

		Map map = new HashMap<String, Object>();
		map.put("sort", "a." + sort);
		map.put("order", order);
		map.put("name", name);
		map.put("state", state);
		map.put("address", address);
		map.put("startime", startime);
		map.put("endtime", endtime);
		map.put("areaid", areaid);

		// Count查询
		try {
			PageInfo<Store> page = storeService.selectByPage(map, pageNum,
					pageSize);

			result.setRows(page.getList());
			result.setTotal(page.getTotal());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping(value = "update/pro/upload")
	@ResponseBody
	public CommonJSON upload(Model model, HttpServletRequest request,HttpServletResponse response) {
		CommonJSON result_json = new CommonJSON();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		try {
			
			List<Map<String, Object>> upload_list = UploadFileUtil.uploadFile(
					request, "store" + File.separator, "gif,jpg,bmp,jpeg,png");
			if (upload_list != null && upload_list.size() > 0) {
				Map<String, Object> upload_map = upload_list.get(0);
				boolean iserror = upload_map.containsKey("error");
				if (iserror) {
					result_json.setSuccess(false);
					result_json.setMessage((String) upload_map.get("error"));
				
				} else {
					result_json.setSuccess(true);
					result_json.setMessage((String) upload_map.get("filePath"));
				  
				}
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			result_json.setSuccess(false);
			result_json.setMessage("图片上传错误");
		}
		
		
		
		return result_json;
	}
}
